//
//  FirstViewController.m
//  UI10_UITableViewHomework
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()
<
    UITableViewDataSource,
    UITableViewDelegate,
    SecondViewControllerDelegate
>

@property (nonatomic,retain) NSMutableDictionary *dictionary;

@property (nonatomic,retain) NSArray *keysArray;

@property (nonatomic,retain) UITableView *tableView;
@end

@implementation FirstViewController
-(void)dealloc{
    [_dictionary release];
    [_keysArray release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化字典和数组
    self.dictionary = [NSMutableDictionary dictionary];
    self.keysArray = [NSArray array];
    
    self.view.backgroundColor = [UIColor grayColor];
    self.title = @"联系人";
    
    [self handleData];
    [self createTableView];
}

-(void)handleData{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Contact" ofType:@"plist"];
    self.dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    self.keysArray = [[self.dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
    
}


-(void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 100.0;
    [self.tableView release];
    self.automaticallyAdjustsScrollViewInsets = NO;
    /** 指定tableView两个协议的代理人为FirstViewController */
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    
}

//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = [self.keysArray objectAtIndex:section];
    NSArray *array = [self.dictionary valueForKey:key];
    return array.count;
}

//分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.keysArray.count;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [self.keysArray objectAtIndex:section];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    /** 从重用池中拿tableViewCell */
    UITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    /** 获取姓名并添加 */
    NSArray *array = [self.dictionary valueForKey:[self.keysArray objectAtIndex:indexPath.section]];
    NSDictionary *dic = [array objectAtIndex: indexPath.row];
    NSString *name = [dic valueForKey:@"name"];
    tableViewCell.textLabel.text = name;
    
    
//    if ([[dic valueForKey:@"header"] isKindOfClass:[UIImage class]]) {
//        tableViewCell.imageView.image = [dic valueForKey:@"header"];
//    } else {
    /** 获取图片并添加 */
    UIImage *image = [UIImage imageNamed:[dic valueForKey:@"header"]];
    /** 将长方形的图片拉伸成正方形 */
    UIImage *newImage = [self reSizeImage:image andSize:CGSizeMake(tableView.rowHeight, tableView.rowHeight)];
    
    /** 使用圆角修正图片层的形状 */  //因为用的图内容是六边形..修不成圆形- -||
    tableViewCell.imageView.layer.masksToBounds = YES;
    tableViewCell.imageView.layer.cornerRadius = tableView.rowHeight * 0.5;
//    tableViewCell.imageView.layer.cornerRadius = tableView.rowHeight * 0.5 + 20;
   
    /** 把修成正方形的图片添加到cell的imageView中 */
    tableViewCell.imageView.image = newImage;
    
 //   }
    
    return tableViewCell;
}

-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.keysArray;
}
/** 属性传值 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SecondViewController *SecondVC = [[SecondViewController alloc] init];
    NSString *key = [self.keysArray objectAtIndex:indexPath.section];
    NSArray *array = [self.dictionary valueForKey:key];
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    
    SecondVC.name = [dic valueForKey:@"name"];
    SecondVC.number = [dic valueForKey:@"number"];
    SecondVC.qq = [dic valueForKey:@"QQ"];
    SecondVC.header = [dic valueForKey:@"header"];
    SecondVC.indexPath = indexPath;
    
    /** 指定代理人 */
    SecondVC.delegate = self;
    
    [self.navigationController pushViewController:SecondVC animated:YES];
}

/** 返回传值的协议方法 */
-(void)backToFirstViewAndChangeCellWithName:(NSString *)name withNumber:(NSString *)number withQQ:(NSString *)qq withHeader:(NSString *)header withIndexPath:(NSIndexPath *)indexPath{
    
    NSString *key = [self.keysArray objectAtIndex: indexPath.section];
    NSArray *array = [self.dictionary valueForKey:key];
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    
    [dic setValue:name forKey:@"name"];
    [dic setValue:number forKey:@"number"];
    [dic setValue:qq forKey:@"QQ"];
    [dic setValue:header forKey:@"header"];
    
    
    
    /** 刷新数据 */
    [self.tableView reloadData];
    
}

-(void)backImage:(UIImage *)image withIndexPath:(NSIndexPath *)indexPath{
    NSString *key = [self.keysArray objectAtIndex: indexPath.section];
    NSArray *array = [self.dictionary valueForKey:key];
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    
  //  [dic setValue:image forKey:@"header"];
    
    [self.tableView reloadData];
}



/** 与数据无关的一些方法 */

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30.0;
}



/** 拉伸图片的方法 */
- (UIImage *) reSizeImage:(UIImage *)image andSize:(CGSize)reSize {
    
    UIGraphicsBeginImageContext(CGSizeMake(reSize.width,reSize.height));
    
    [image drawInRect:CGRectMake(0, 0, reSize.width,reSize.height)];
    
    UIImage *reSizeImage =UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return reSizeImage;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
